//
//  WebViewController.m
//  muce
//
//  Created by  yunshao on 2018/2/8.
//  Copyright © 2018年  yunshao. All rights reserved.
//

#import "WebViewController.h"
#import "pch.h"
#import <WebKit/WebKit.h>

@interface WebViewController ()<WKUIDelegate,WKScriptMessageHandler,WKNavigationDelegate>{
    
}
@property(nonatomic,strong)WKWebView * webView;                 //网页

@property(nonatomic,strong)UIView * backView;
@property(nonatomic,strong)UIButton * backButton;               //返回按钮
@property(nonatomic,strong)UILabel * titleLable;                //标题


@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addMianView];
}

-(void)addMianView{
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DSDevice_W, DSFixedPX(110))];
    _backView.backgroundColor = DSColorWithHex(0x3279ca);
    _backView.userInteractionEnabled = YES;
    [self.view addSubview:_backView];
    
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(DSFixedPX(20), DSFixedPX(40), DSFixedPX(50), DSFixedPX(50));
     _backButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    [_backButton setImage:[UIImage imageNamed:@"title_back"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:_backButton];
    _titleLable = [[UILabel alloc]initWithFrame:CGRectMake(DSFixedPX(50), DSFixedPX(40), DSDevice_W - DSFixedPX(100), DSFixedPX(50))];
    _titleLable.text = self.titleString;
    _titleLable.textAlignment = NSTextAlignmentCenter;
    _titleLable.font = DSFontSize(16);
    _titleLable.textColor = DSColorWithHex(0xffffff);
    [_backView addSubview:_titleLable];
    
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_backView.frame), DSDevice_W, DSDevice_H  - DSFixedPX(110))];
    _webView.navigationDelegate = self;
    _webView.UIDelegate = self;
    _webView.allowsBackForwardNavigationGestures = YES;
    [self.view addSubview:_webView];
    
    if (!self.urlString || self.urlString.length <= 0) {
        return;
    }
    NSURL *webUrl = [NSURL URLWithString:self.urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:webUrl];
    request.timeoutInterval = DSTimeOut;
    [_webView loadRequest:request];
    
}
-(void)backButtonClicked{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"开始加载网页");
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    NSLog(@"加载完成");
}
//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"加载失败");
}


#pragma mark - WKScriptMessageHandler
/*
 1、js调用原生的方法就会走这个方法
 2、message参数里面有2个参数我们比较有用，name和body，
 2.1 :其中name就是之前已经通过addScriptMessageHandler:name:方法注入的js名称
 2.2 :其中body就是我们传递的参数了，我在js端传入的是一个字典，所以取出来也是字典，字典里面包含原生方法名以及被点击图片的url
 */
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if([message.name isEqualToString:@"Eixtweb"]) // 返回
    {
        // pan??
        //        [self goBack:nil];
    }
}
#pragma mark - webViewDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
    decisionHandler(WKNavigationResponsePolicyAllow);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
